package io.github.junxworks.qt.modules.strategy.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.github.junxworks.ep.auth.model.UserModel;
import io.github.junxworks.ep.sys.constants.RecordStatus;
import io.github.junxworks.qt.modules.strategy.dto.SStrategyCondition;
import io.github.junxworks.qt.modules.strategy.dto.SStrategyDto;
import io.github.junxworks.qt.modules.strategy.entity.SStrategy;
import io.github.junxworks.qt.modules.strategy.mapper.SStrategyMapper;
import io.github.junxworks.qt.modules.strategy.service.SStrategyService;
import io.github.junxworks.qt.modules.strategy.vo.SStrategyVo;

@Service
public class SStrategyServiceImpl implements SStrategyService {

	@Autowired
	private SStrategyMapper sStrategyMapper;

	@Override
	public List<SStrategyVo> queryList(SStrategyCondition condition) {
		return sStrategyMapper.queryList(condition);
	}

	@Override
	public SStrategyVo querySStrategyById(Long id) {
		return sStrategyMapper.querySStrategyById(id);
	}

	@Override
	public void save(SStrategyDto dto) {
		SStrategy entity = new SStrategy();
		BeanUtils.copyProperties(dto, entity);
		UserModel user = (UserModel) SecurityUtils.getSubject().getPrincipal();
		Long userId = user.getId();
		if (entity.getId() == null) {
			entity.setCreateTime(new Date());
			entity.setCreateUser(userId);
			entity.setStatus(RecordStatus.NORMAL.getValue());
			sStrategyMapper.insertWithoutNull(entity);
		} else {
			entity.setUpdateTime(new Date());
			entity.setUpdateUser(userId);
			sStrategyMapper.updateWithoutNull(entity);
		}
	}

	@Override
	public List<SStrategyVo> querySubStrategyList(SStrategyCondition condition) {
		return sStrategyMapper.querySubStrategyList(condition);
	}
}
